package com.wechat.admin.controller;

import com.wechat.admin.annoation.AuthCheck;
import com.wechat.admin.common.BaseResponse;
import com.wechat.admin.common.CommonConstants;
import com.wechat.admin.common.ResultUtils;
import com.wechat.admin.exception.ErrorCode;
import com.wechat.admin.exception.ThrowUtils;
import com.wechat.admin.model.request.*;
import com.wechat.admin.model.vo.*;
import com.wechat.admin.response.PageResponse;
import com.wechat.admin.service.WeDeviceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @Author:sjb
 * @CreateTime:2024-12-15
 * @Description: 系统内部的微信相关的视图
 * @Version：1.0
 */
@RestController
@RequestMapping("/weChat")
public class WeChatController {

    @Resource
    private WeDeviceService weDeviceService;

    /**
     * 登录时获取二维码
     *
     * @param getLoginQrCodeRequest
     * @return
     */
    @PostMapping("/getLoginQrCode")
    public BaseResponse<LoginQrCodeVo> getLoginQrCodeVo(@RequestBody GetLoginQrCodeRequest getLoginQrCodeRequest) {
        ThrowUtils.throwIf(getLoginQrCodeRequest == null, ErrorCode.PARAMS_ERROR);

        LoginQrCodeVo loginQrCode = weDeviceService.getLoginQrCode(getLoginQrCodeRequest);
        return ResultUtils.success(loginQrCode);
    }

    /**
     * 前端每隔5s请求一次该接口
     * 根据 weChatLoginStatus 判断是否登录成功
     * 扫码完成之后的登录请求
     */
    @PostMapping("/weChatLogin")
    public BaseResponse<WeChatLoginInfoVo> weChatLoginByQrCode(@RequestBody WeChatLoginRequest weChatLoginRequest) {
        ThrowUtils.throwIf(weChatLoginRequest == null, ErrorCode.PARAMS_ERROR);
        WeChatLoginInfoVo weChatLoginInfoVo = weDeviceService.weChatLogin(weChatLoginRequest);
        return ResultUtils.success(weChatLoginInfoVo);
    }

    /**
     * 检查是否在线
     *
     * @return
     */
    @PostMapping("/checkOnline")
    public BaseResponse<Map<String, Boolean>> checkOnline(@RequestBody CheckOnlineRequest checkOnlineRequest) {
        ThrowUtils.throwIf(checkOnlineRequest == null, ErrorCode.PARAMS_ERROR);

        Map<String, Boolean> stringBooleanMap = weDeviceService.checkOnline(checkOnlineRequest);
        return ResultUtils.success(stringBooleanMap);
    }

    /**
     * 查询可使用Token
     *
     * @return
     */
    @GetMapping("/getUsableToken")
    public BaseResponse<List<DockerTokenVo>> getUsableToken() {
        List<DockerTokenVo> usableToken = weDeviceService.getUsableToken();
        return ResultUtils.success(usableToken);
    }

    /**
     * 申请完token后 看到可以使用的设备登录
     *
     * @return
     */
    @GetMapping("/getUsableDevice")
    public BaseResponse<List<UsableDeviceVo>> getUsableDevice() {
        List<UsableDeviceVo> usableDeviceList = weDeviceService.getUsableDeviceList();
        return ResultUtils.success(usableDeviceList);
    }

    /**
     * 管理员获取已分配设备列表
     *
     * @return
     */
    @GetMapping("/getAssignmentDevice")
    @AuthCheck(mustRole = CommonConstants.ADMIN)
    public BaseResponse<AssignmentDeviceVo> getAssignmentDevice(@RequestParam(defaultValue = "1") int currentPage,
                                                                @RequestParam(defaultValue = "10") int pageSize) {
        AssignmentDeviceVo assignmentDeviceList = weDeviceService.
                getAssignmentDeviceList(currentPage, pageSize);
        return ResultUtils.success(assignmentDeviceList);
    }

    /**
     * 微信登出
     *
     * @param weChatLogOutRequest
     * @return
     */
    @PostMapping("/weChatLogout")
    public BaseResponse<Boolean> weChatLogOut(@RequestBody WeChatLogOutRequest weChatLogOutRequest) {
        ThrowUtils.throwIf(weChatLogOutRequest == null, ErrorCode.PARAMS_ERROR);
        Boolean logout = weDeviceService.weChatLogout(weChatLogOutRequest);
        return ResultUtils.success(logout);
    }

    @PostMapping("/delDeviceAndUserRel")
    @AuthCheck(mustRole = CommonConstants.ADMIN)
    public BaseResponse<Boolean> delDeviceAndUserRel(@RequestBody DelDeviceAndUserRelRequest delDeviceAndUserRelRequest) {
        ThrowUtils.throwIf(delDeviceAndUserRelRequest == null, ErrorCode.PARAMS_ERROR);
        Boolean devRelation = weDeviceService.delUserAndDevRelation(delDeviceAndUserRelRequest);
        return ResultUtils.success(devRelation);
    }
}
